home *** CD-ROM | disk | FTP | other *** search
- package symantec.itools.db.net;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Vector;
- import symjava.sql.SQLException;
-
- public final class BinaryOutputStream extends OutputStream {
- private final int METHOD_getData;
- private final int METHOD_setData = 1;
- private final int METHOD_rewind = 2;
- private final int METHOD_setNull = 3;
- private RemoteObject proxy;
- private byte[] _data;
- private int offset;
- private boolean bClosed;
- private final int MAX_BUF_SIZE = 4000;
-
- public BinaryOutputStream(int id, ClientSession session) throws SQLException {
- this.proxy = new RemoteObject("ProxyStream", id, session);
- this._data = new byte[4000];
- this.offset = 0;
- this.bClosed = false;
- this.proxy.invokeMethod(2);
- }
-
- public void sendData(InputStream in, int len) throws SQLException {
- if (this.bClosed) {
- throw new SQLException("Stream is closed.");
- } else {
- try {
- this.flush();
-
- while(len > 4000) {
- in.read(this._data, 0, 4000);
- this.offset = 4000;
- this.flush();
- len -= 4000;
- }
-
- in.read(this._data, 0, len);
- this.offset = len;
- this.flush();
- } catch (IOException e) {
- throw new SQLException(((Throwable)e).getMessage());
- }
- }
- }
-
- public void close() throws IOException {
- this.bClosed = true;
- }
-
- public synchronized void write(int b) throws IOException {
- if (this.bClosed) {
- throw new IOException("Stream is closed.");
- } else {
- if (this.offset == this._data.length) {
- this.flush();
- }
-
- this._data[this.offset++] = (byte)b;
- }
- }
-
- public synchronized void write(byte[] b, int off, int len) throws IOException {
- if (this.bClosed) {
- throw new IOException("Stream is closed.");
- } else {
- int avail = this._data.length - this.offset;
- if (len <= avail) {
- System.arraycopy(b, off, this._data, this.offset, len);
- this.offset += len;
- } else {
- this.flush();
- byte[] temp = new byte[len];
- System.arraycopy(temp, 0, b, off, len);
- this.setData(temp, len);
- }
- }
- }
-
- public synchronized void flush() throws IOException {
- this.setData(this._data, this.offset);
- this.offset = 0;
- }
-
- void setData(byte[] b, int len) throws IOException {
- if (len != 0) {
- Vector params = new Vector();
- params.addElement(new Param(0, len));
- params.addElement(new Param(0, b, len));
-
- try {
- this.proxy.invokeMethod(1, params);
- } catch (Exception e) {
- throw new IOException(((Throwable)e).getMessage());
- }
- }
- }
-
- public void setNull() throws SQLException {
- this.proxy.invokeMethod(3);
- }
-
- public void setBytes(byte[] data) throws SQLException {
- try {
- this.flush();
- this.setData(data, data.length);
- } catch (IOException e) {
- throw new SQLException(((Throwable)e).getMessage());
- }
- }
-
- public void setByte(byte data) throws SQLException {
- try {
- this.flush();
- byte[] b = new byte[1];
- b[0] = data;
- this.setData(b, 1);
- } catch (IOException e) {
- throw new SQLException(((Throwable)e).getMessage());
- }
- }
-
- public void setShort(short data) throws SQLException {
- try {
- this.flush();
- NetData d = new NetData(data);
- byte[] b = d.getBytes();
- this.setData(b, b.length);
- } catch (IOException e) {
- throw new SQLException(((Throwable)e).getMessage());
- }
- }
-
- public void setBoolean(boolean data) throws SQLException {
- try {
- this.flush();
- NetData d = new NetData(data);
- byte[] b = d.getBytes();
- this.setData(b, b.length);
- } catch (IOException e) {
- throw new SQLException(((Throwable)e).getMessage());
- }
- }
-
- public void setInt(int data) throws SQLException {
- try {
- this.flush();
- NetData d = new NetData(data);
- byte[] b = d.getBytes();
- this.setData(b, b.length);
- } catch (IOException e) {
- throw new SQLException(((Throwable)e).getMessage());
- }
- }
-
- public void setLong(long data) throws SQLException {
- try {
- this.flush();
- NetData d = new NetData(data);
- byte[] b = d.getBytes();
- this.setData(b, b.length);
- } catch (IOException e) {
- throw new SQLException(((Throwable)e).getMessage());
- }
- }
-
- public void setFloat(float data) throws SQLException {
- try {
- this.flush();
- NetData d = new NetData(data);
- byte[] b = d.getBytes();
- this.setData(b, b.length);
- } catch (IOException e) {
- throw new SQLException(((Throwable)e).getMessage());
- }
- }
-
- public void setDouble(double data) throws SQLException {
- try {
- this.flush();
- NetData d = new NetData(data);
- byte[] b = d.getBytes();
- this.setData(b, b.length);
- } catch (IOException e) {
- throw new SQLException(((Throwable)e).getMessage());
- }
- }
-
- public void setString(String data) throws SQLException {
- try {
- this.flush();
- NetData d = new NetData(data);
- byte[] b = d.getBytes();
- this.setData(b, b.length);
- } catch (IOException e) {
- throw new SQLException(((Throwable)e).getMessage());
- }
- }
- }
-